package cm.mefobe.android.telecomcost.view;

import cm.mefobe.android.telecomcost.entities.WeeklyThreshold;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TableRow;
import android.widget.TextView;

public class WeeklyThresholdView extends TableRow {

	private Context _context;
	private TextView _lblAmount;
	private WeekView weekView;
	private WeeklyThreshold _threshold;
	
	public void setWeeklyThreshold(WeeklyThreshold threshold) {
		this._threshold = threshold;
		setWeeklyThresholdView();
	}

	public WeeklyThreshold getWeeklyThreshold() {
		return _threshold;
	}
	
	public WeeklyThresholdView(Context context,WeeklyThreshold threshold) {
		super(context);
		Log.i("WeeklyThresholdView", "creating the WeeklyThresholdView");
		_context = context;
		setWeeklyThreshold(threshold);		
	}		

	private void setWeeklyThresholdView() {
		if (isInEditMode()) {
			Log.i("WeeklyThresholdView", "isInEditMode");
		} else {
			if (_lblAmount == null && _threshold != null) {
				Log.i("WeeklyThresholdView", "adding views...");				

				_lblAmount = new TextView(_context);				
				weekView = new WeekView(_context,_threshold.getWeek());								

				addView(weekView);
				addView(_lblAmount);				
				setThreshold();
			} else {
				Log.i("WeeklyThresholdView", "WeeklyThresholdView already set...");
			}
		}
	}

	private void setThreshold() {
		if(_lblAmount != null && weekView != null)
		{
			if(_threshold != null)
			{
				_lblAmount.setText(String.valueOf(_threshold.getAmount()));	
				weekView.setWeek(_threshold.getWeek());
			}		
			else
			{
				Log.e("WeeklyThresholdView", "cannot setThreshold..._threshold is null");
			}
		}
		else
		{
			Log.e("WeeklyThresholdView", "cannot setThreshold..._lblAmount or weekView null");
		}
	}

	public WeeklyThresholdView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("WeeklyThresholdView", "creating the WeeklyThresholdView with AttributeSet");
		_context = context;		
		//setWeeklyThresholdView();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("WeeklyThresholdView", "onLayout");
		super.onLayout(changed, l, t, r, b);
		setWeeklyThresholdView();
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("WeeklyThresholdView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("WeeklyThresholdView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("WeeklyThresholdView", "onFinishInflate");
		super.onFinishInflate();
		setWeeklyThresholdView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("WeeklyThresholdView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}	

	public WeekView getWeekView() {
		if (weekView == null) {
			throw new UnsupportedOperationException(
					"WeeklyThresholdView weekView is not set");
		}
		return weekView;
	}	
}